<?php

namespace App\Http\Controllers;

use App\Common\LogUtil;
use App\Common\Models\Db1\MpApp;
use App\Common\Models\Db1\MpCallbackContent;
use App\Common\Models\Db1\MpGood;
use App\Common\Util;
use App\Common\WxUtil;
use Illuminate\Http\Request;
use EasyWeChat\Factory;
use EasyWeChat;
use Log;

class Good2Controller extends Controller
{
    //
    public function list()
    {
        $get = $_GET;
        if (empty($get['app_id'])) {
            return '请从公众号菜单打开';
        }

        //获取openid
        if (empty($get['openid'])) {
            global $wx_config;
            $wx_config = WxUtil::getWxConfig($get['app_id']);
            if (empty($wx_config['app_id'])) {
                return json_encode($wx_config);
                return 'app信息为空';
            }
            require_once app_path() . '/Common/Lib/php_sdk_v3.0.10/example_modify/jsapi.php';
            $openid = jsapi_get_openid();
            $get['openid'] = $openid;
            //return $openid;
        }

        $rows = (new MpGood())->findAll(['on_sale' => 1], 0, 20, 'id desc');
        $ret = [
            'code' => 0,
            'msg' => '',
            'total' => 0,
            'data' =>
                $rows
        ];
        //return Util::json_encode_unicode($ret);

        return view('good2/list', ['rows' => $rows, 'get' => $get]);
    }

    public function detail()
    {
        $id = $_GET['id'] ?? '';
        $get = $_GET;
        $item_good = (new MpGood())->findOne(['id' => $id]);

        $pay_data = [];
        global $wx_config;
        $wx_config = WxUtil::getWxConfig($get['app_id']);
        if (empty($wx_config['app_id'])) {
            return json_encode($wx_config);
            return 'app信息为空';
        }
        require_once app_path() . '/Common/Lib/php_sdk_v3.0.10/example_modify/jsapi.php';
        $pay_data = jsapi_order($get['openid'], $id);
        //LogUtil::info('test', $pay_data, __FILE__, __LINE__);
        return view('good2/detail', ['row' => $item_good, 'get' => $get, 'pay_data' => $pay_data]);
    }
}
